package org.sp1.service;

import java.util.Date;

import org.sp1.dao.LoginDao;
import org.sp1.dao.UserDao;
import org.sp1.model.SpLoginLog;
import org.sp1.model.SpLoginLogId;
import org.sp1.model.SpUser;

public class UserServiceImpl implements UserService{
	
	private LoginDao loginDao;
	private UserDao userDao;

	public SpUser findUserByUserName(String userName) {
		// TODO 自动生成方法存根
		
		return userDao.findByUserName(userName);
	}

	public boolean hasMatchUser(String userName, String password) {
		// TODO 自动生成方法存根
		int Count=userDao.getMacthCount(userName, password);
		
		return Count>0;
	}

	public void loginSuccess(SpUser user) {
		// TODO 自动生成方法存根
		
		user.setCredits(5+user.getCredits());
		SpLoginLog loginLog=new SpLoginLog();
		
		SpLoginLogId ID =loginLog.getId();
		
		ID.setUserId(user.getUserId());
		ID.setIp(user.getLastIp());
		ID.setLoginDatetime(user.getLastVisit());

		//loginLog.setId(ID);
		
		userDao.updateLoginInfo(user);
		loginDao.insertLoninLog(loginLog);
		
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}
